package com.ck.framework.aop;

import com.ck.common.utils.txt.TextUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author ck
 * @date 2023/3/15 14:35
 */


/**
 * 定时任务日志记录
 * @author ck
 */

@Aspect
@Component
@Order(1)
@Slf4j
public class LogTaskAspect {
    @Pointcut("@annotation(com.ck.common.annotation.TaskLog)")
    private void taskPointCut() {
    }

    @Around(value = "taskPointCut()", argNames = "point")
    public Object doLogger(ProceedingJoinPoint point) {
        long start = System.currentTimeMillis();
        String name = point.getTarget().getClass().getName();
        log.debug("{} 开始执行", name);
        Object proceed = null;
        try {
            proceed = point.proceed();
        } catch (Throwable throwable) {
            log.error("出现异常:{}", TextUtil.ex2Txt(throwable));
        } finally {
            long end = System.currentTimeMillis();
            log.debug("{} 执行时间为:{} 秒", name, (end - start) / 1000.0);
        }
        return proceed;
    }
}
